<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="projectName"
          required
          label="框架类型名称">
          <a-input placeholder="请输入框架类型名称" v-model="model.projectName"/>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="javaPath"
          required
          label="java根包路径">
          <a-input placeholder="请输入java根包路径" v-model="model.javaPath"/>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="templateIds"
          required
          label="代码模板">
          <j-multi-select-tag  placeholder="请选择代码模板" v-model="model.templateIds" dict-code="moudual_template_config,name,id"></j-multi-select-tag>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: "MoudualPathConfigModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        templateList:[],
        input_width: {width: '270px'},
        confirmLoading: false,
        validatorRules: {
          projectName:[{required: true, message: '请输入框架类型名称!'},
                      {max: 255, message: '长度不能大于255', trigger: 'blur'}],
          javaPath: [{required: true, message: '请输入java根包路径!'},
                    {max: 200, message: '长度不能大于200', trigger: 'blur'}],
          templateIds:[
                {required: true, message: '请选择模板!'},
          ]
        },
        url: {
          add: "/generator/MoudualPathConfig/add",
          edit: "/generator/MoudualPathConfig/edit",
        },
      }
    },
    created() {

    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.model = Object.assign({}, record)
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style lang="less" scoped>

</style>
